package com.lishuang.basic.controller;

import com.lishuang.basic.dto.SmsCodeDto;
import com.lishuang.basic.exception.BusinessException;
import com.lishuang.basic.service.IVerifyService;
import com.lishuang.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码接口
 * 1.图形验证码接口
 * 2.手机验证码接口
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyService verifyService;

    /**
     * 获取图形验证码
     * @param key
     * @return
     */
    @RequestMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyService.getImgCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e){ //业务异常捕获
            return AjaxResult.me().setSuccess(false).setMessage("获取失败!"+e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }
    }

    /**
     * 获取手机验证码接口
     * @param smsCodeDto
     * @return
     */

    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyService.sendSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }
    }

    /**
     * 获取手机验证码：微信绑定
     * @param binderSmsCode
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyService.binderSmsCode(smsCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }
    }

}